package cn.butcher.controller;

import cn.butcher.service.StudentService;
import cn.butcher.vo.LeaveForm;
import cn.butcher.vo.StudentLeaveMsg;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * 申请请假控制器
 */
@Controller
public class StudentController {


    @Autowired
    StudentService studentService;

    @RequestMapping("/student/getName/{id}")
    @ResponseBody
    public String getName(@PathVariable("id") int id) {
        return studentService.getNameById(id);
    }

    // 这种在路径里面放参数的写法叫：restFul风格
    // 传统的get请求是这样的：ip:8080?username=zhangsan&password=zs123
    // restFul风格请求是这样的：ip:8080/zhangsan/zs123

    @RequestMapping("/student/getTeacherName/{cId}")
    @ResponseBody
    public String getTeacherName(@PathVariable("cId") int cId) {
        return studentService.getTeacherNameById(cId);
    }

    @RequestMapping("/student/apply/commit")
    @ResponseBody
    //@RequestParam("stId") int stId
    public String commit(LeaveForm leaveForm) {
        studentService.insertLeave(leaveForm);
        return "恭喜你，请假申请提交成功！";
    }


    @RequestMapping("/student/cancel")
    public String getStudentLeaveMsg(Model model, HttpSession session) {
        int id = Integer.parseInt((String) session.getAttribute("id"));
        model.addAttribute("list", studentService.getStudentLeaveMsg('3', id));
        return "page/student-cancel";
    }

    @RequestMapping("/student/record")
    public String studentRecord(Model model, HttpSession session) {
        int id = Integer.parseInt((String) session.getAttribute("id"));
        model.addAttribute("list", studentService.getStudentLeaveMsg('1', id));
        return "page/student-record";
    }


    @RequestMapping("/student/cancel/complete")
    @ResponseBody
    public String cancelLeave(int id) {
        studentService.changeLeaveStatus('6', id);
        return "";
    }

    @RequestMapping("/student/cancel/lay")
    @ResponseBody
    public String layLeave(String reason, String date, int id) {
        studentService.delayLeaveDate(reason, date, id);
        return "";
    }


    @RequestMapping("/student/look")
    public String studentLook(Model model, HttpSession session) {
        int id = Integer.parseInt((String) session.getAttribute("id"));
        model.addAttribute("list", studentService.getStudentLeaveMsg('5', id));
        return "page/student-look";
    }

    @RequestMapping("/send/urge")
    @ResponseBody
    public String sendUrge(int leId) throws TencentCloudSDKException {
        System.out.println("leId=" + leId);
        studentService.sendUrge(leId);
        return "";
    }

}
